#pragma once
// MySQL版本
#include "../Comm/util.hpp"
#include "../Comm/log.hpp"
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <fstream>
#include <cstdlib>
#include <cassert>
#include <mysql/mysql.h>

// MySQL version

// 根据题目list文件，加载所有的题目的题目信息到内存中
// model：主要用来和数据进行交互，对外提供访问的接口

namespace ns_model
{
    const std::string oj_questions = "oj_questions";

    const std::string host = "127.0.0.1";
    const std::string user = "oj_client";
    const std::string password = "123456";
    const std::string db = "oj";
    const unsigned int port = 3306;
    using namespace ns_log;
    using namespace std;
    using namespace ns_util;

    struct Question
    {
        std::string _number; // 题号，唯一
        std::string _title;  // 题目标题
        std::string _star;   // 难度：简单，中等，困难
        std::string _desc;   // 题目描述
        std::string _header; // 题目提前预设给用户在先编译的代码
        std::string _tail;   // 题目的测试用例，需要和header拼接，形成完整代码
        int _cpu_limit;      // 时间限制
        int _mem_limit;      // 空间限制
        std::string test_input;
    };
    class Model
    {
    public:
        Model()
        {}
        bool QueryMySql(const std::string &sql, vector<Question> *out)
        {
            // 连接数据库
            MYSQL *my = mysql_init(nullptr);
            
            if (nullptr == mysql_real_connect(my, host.c_str(),user.c_str(),password.c_str(),db.c_str(),port, nullptr, 0))
            {
                LOG(FATAL) << "数据库连接失败" << "\n";
                return false;
            }
            mysql_set_character_set(my, "utf8");
            LOG(INFO) << "连接数据库成功！" << "\n";

            // 执行sql语句
            if (0 != mysql_query(my, sql.c_str()))
            {
                LOG(WARNING) << mysql_error(my) << "\n";
                return false;
            }

            // 提取结果
            MYSQL_RES* res = mysql_store_result(my);

            // 分析结果
            uint64_t rows = mysql_num_rows(res); // 获取行数量
            unsigned int cols = mysql_num_fields(res); // 获取列数量
            struct Question q;
            for (int i = 0; i < rows; i++)
            {
                MYSQL_ROW row = mysql_fetch_row(res);
                q._number = row[0];
                q._title = row[1];
                q._star = row[2];
                q._desc = row[3];
                q._header = row[4];
                q._tail = row[5];
                q._cpu_limit = atoi(row[6]);
                q._mem_limit = atoi(row[7]);
                std::string path = "./questions/";
                path += q._number;
                path += "/";
                FileUtil::ReadFile(path + "test_input.cpp", &(q.test_input), true);
                out->push_back(q);
            }

            //释放结果集
            mysql_free_result(res);
            // 关闭连接
            mysql_close(my);
            return true;
        }
        bool GetAllQuestions(vector<Question> *out) // 获取全部数据
        {
            std::string sql = "select * from ";
            sql += oj_questions;
            return QueryMySql(sql, out);
        }
        bool GetOneQuestion(const string &number, Question *q) // 获取一道题目
        {
            bool res = false;
            std::string sql = "select * from ";
            sql += oj_questions;
            sql += " where number=";
            sql += number;
            vector<Question> result;
            if (QueryMySql(sql, &result))
            {
                if (result.size() == 1)
                {
                    *q = result[0];
                    res = true;
                }
            }
            return res;
        }

        ~Model() {}
    };
} // namespace ns_model